package longtdbk.android.job.data;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URI;

import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;



import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xmlrpc.android.XMLRPCClient;
import org.xmlrpc.android.XMLRPCException;

import com.google.android.Constants;

import android.util.Log;


import longtdbk.android.job.data.JobData;


public class JobFetcher {
    private static final String CLASSTAG = JobFetcher.class.getSimpleName();

    private static final String QBASE = "http://longtdbk.appspot.com/xmlrpc/";
    private static final String QMETHOD = "app.getCompany";
    private String query;
    private String lat;
    private String lon;
    private String rad;
	private XMLRPCClient client;
	private URI uri;
    /**
     * Network data fetcher for NDBC RSS feeds, requires lat in such as 37W, and long in such as
     * 122E (not integers or doubles, Strings).
     * 
     * @param lat
     * @param lon
     * @param rad
     */
    public JobFetcher(String lat, String lon, String rad) {
        this.lat = lat;
        this.lon = lon;
        this.rad = rad;

        this.query = JobFetcher.QBASE ;
        Log.i(Constants.LOGTAG, JobFetcher.CLASSTAG + " query - " + this.query);
    }

    public ArrayList<JobData> getData() {
		uri = URI.create(QBASE);
		client = new XMLRPCClient(uri);
		ArrayList<JobData> jobs = new ArrayList<JobData>();
		try {
			JobData job = new JobData();
			final  String result =  (String)client.call(QMETHOD,lat,lon,rad); 
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse( new ByteArrayInputStream(result.getBytes("UTF-8")));
			//Document doc=db.parse(new InputSource(new StringReader(result)));
			NodeList nodes_param= doc.getElementsByTagName("param");
            for (int i = 0; i < nodes_param.getLength(); i++) {
                  Node paramNode = nodes_param.item(i);
                  Element paramElement = (Element) paramNode;
                  NodeList nodes_member = paramElement.getElementsByTagName("member");
                  for(int j = 0; j< nodes_member.getLength(); j++){
                	  Node memberNode = nodes_member.item(j);
                	  Element memberElement = (Element) memberNode;
                	  NodeList node_values = memberElement.getElementsByTagName("value");
                	  Node valueNode = node_values.item(0);
                	  if(j == 0){
                		  job.phone = valueNode.getTextContent();
                	  }
                	  if (j == 1){
                		  job.company = valueNode.getTextContent();
                	  }
                	  if (j == 2){
                		  job.location = valueNode.getTextContent();
                	  }	
                  }
                  jobs.add(job);
            }	

		} catch (XMLRPCException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();	
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return jobs;
    }
}
